/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.core.internal.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

/**
  * @version $Revision: 89 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class MetaFileWriter 
{
	// ---------------------------------------------------------------------------- Public Constants

	/** the local workspace file that contains pattern for ignored resources */
	public static final String IGNORE_FILE = ".p4ignore";                              //$NON-NLS-1$

	// ----------------------------------------------------------------------- Public Static Methods

	/** Reads the ignore file for the given folder and returns its entries.
	  *
	  * @param folder the folder.
	  * @return the ignore entries.
	  * @throws PerforceException in case of an error. 
	  */
	public static String[] readP4IgnoreEntries(IContainer folder) 
		throws PerforceException 
	{
		IFile ignoreFile = folder.getFile(new Path(IGNORE_FILE));
		if (ignoreFile != null) 
		{
			return readLines(ignoreFile);
		}
		
		return null;
	}	
	
	// ---------------------------------------------------------------------- Private Static Methods
	
	/** Called to read the content of the given file.
	  *
	  * @param file the to read.
	  * @return the contents file. 
	  * @throws PerforceException in case of an error.
	  */ 
	private static String[] readLines(IFile file) 
		throws PerforceException 
	{
		try 
		{
			BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents()));
			List fileContentStore = new ArrayList();
			try 
			{
				String line;
				while ((line = reader.readLine()) != null) 
				{
					fileContentStore.add(line);
				}
				
				return (String[]) fileContentStore.toArray(new String[fileContentStore.size()]);
			} 
			finally 
			{
				reader.close();
			}
		} 
		catch (IOException e) 
		{
			throw new PerforceException(
				new Status(Status.ERROR, PerforceCorePlugin.ID, -1, e.getMessage(), e));
		}
		catch (CoreException e) 
		{
			if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND)
			{
				return null;
			}
			
			throw new PerforceException(e.getStatus());
		}  
	}		
}
